package com.unionpay.upyzt.example;

import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.WithdrawableDeposit;
import com.unionpay.upyzt.resp.WithdrawableDepositResp;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

public class WithdrawableDepositExample {

    private String balanceAcctId;

    public WithdrawableDepositExample(String balanceAcctId) {
        this.balanceAcctId = balanceAcctId;
    }

    public static void runDemos(String balanceAcctId) throws UpyztException {
        WithdrawableDepositExample example = new WithdrawableDepositExample(balanceAcctId);
        // 可提现充值
        WithdrawableDepositResp withdrawableDepositResp = example.create();
        // 可提现充值订单查询（系统订单号）
        withdrawableDepositResp = example.retrieveById(withdrawableDepositResp.getWithdrawableDepositId());
        // 可提现充值订单查询（平台订单号）
        withdrawableDepositResp = example.retrieveByOutOrderNo(withdrawableDepositResp.getOutOrderNo());
    }

    private WithdrawableDepositResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("amount", 100);
        params.put("balance_acct_id", balanceAcctId);
        params.put("deposit_type", "1");
        params.put("payment_type", "SDK_TEST");
        params.put("payment_trade_no", Main.generateRandomOutNo());
        params.put("payment_succeeded_at", OffsetDateTime.now().toString());
        params.put("remark", "用于 SDK 示例测试");
        params.put("extra", MockData.extra());
        params.put("metadata", MockData.metadata());

        System.out.println("------- 可提现充值  -------");
        WithdrawableDepositResp resp = WithdrawableDeposit.create(params);
        System.out.println(resp);
        return resp;
    }

    private WithdrawableDepositResp retrieveById(String withdrawableDepositId) throws UpyztException {
        System.out.println("------- 可提现充值订单查询（系统订单号）  -------");
        WithdrawableDepositResp resp = WithdrawableDeposit.retrieveById(withdrawableDepositId);
        System.out.println(resp);
        return resp;
    }

    private WithdrawableDepositResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 可提现充值订单查询（平台订单号）  -------");
        WithdrawableDepositResp resp = WithdrawableDeposit.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }
}
